import { WaterMark }  from '@lowcode/jpaas-base-lib';
import PublicApi from "@/common/api/public";
import userUser from "@/stores/userUser.js";
import { mapState } from 'pinia';

export default {
    data() {
        return {
            waterMarkConfig: {
                "rotate":"-30",
                "widthVal":360,
                "fontSize":20,
                "colorVal":"#c8c8c8",
                "content":"",
                "heightVal":270
            },
            curUserJson:{}
        }
    },
    mounted() {
        //添加水印
        this.getWaterMarkConfig();
    },
    computed:{
        ...mapState(userUser,['user'])
    },
    methods: {
        getWaterMarkConfig() {
            let self_ = this;
            PublicApi.getWaterMarkConfig().then(res => {
                var data = res.data;
                if (!data ) {
                    return;
                }
                let configs=JSON.parse(data.contFig);
                if (!configs.isWatermark || !configs.content) {
                    return;
                }
                self_.waterMarkConfig = {...self_.waterMarkConfig, ...configs};
                setTimeout(function (){
                    self_.initWaterMark();
                }, 1000);
            });
        },
        initWaterMark() {
            if (!this.waterMarkConfig.rotate) {
                this.waterMarkConfig.rotate = 30;
            }
            let userConfig=this.user;
            var reg = /\{\s*(.+?)\s*\}/g;
            var contentVal = this.waterMarkConfig.content.replace(reg, function (a, b) {
                return userConfig[b];
            });
            WaterMark.setByParams(contentVal, this.$refs.container, this.waterMarkConfig);
        },
    },
}
